#coding:utf-8
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from lovewith.account.views.home import validate_user
from lovewith.account.views.modules.userapi import UserApi
from lovewith.analytics.views.modules.analyticsapi import ShopAnalyticsApi
from lovewith.share.views.modules.share import ShareApi
from lovewith.views import site_info
from lovewith.supplier.views.modules.supplierapi import SupplierApi
from lovewith.website.views.modules.province import ProvinceApi


#服务商主页
def supplier_home(request, user_id):
    user_data = UserApi.check_data(user_id=int(user_id))
    if not user_data.group_id == 3:
        return HttpResponseRedirect('/')

    #分享的帖子
    user_share_data = ShareApi().get_post_by_user(user_id, 1)
    supplier_data = SupplierApi.check_data(user_id=int(user_id))

    #增加统计数据
    ShopAnalyticsApi.increase(supplier_data)

    return render_to_response('supplier/home.jinja', {
        'tab': 'home',
        'user_home': supplier_data.user,
        'is_home': validate_user(request.user, supplier_data.user_id),
        'supplier_data': supplier_data,
        'share_data': user_share_data.get('share'),
    }, context_instance=RequestContext(request, processors=[site_info]))


#关于服务商
def about_supplier(request, user_id):
    user_data = UserApi.check_data(user_id=int(user_id))
    if not user_data.group_id == 3:
        return HttpResponseRedirect('/')

    supplier_data = SupplierApi.check_data(user_id=int(user_id))

    #增加统计数据
    ShopAnalyticsApi.increase(supplier_data)

    return render_to_response('supplier/about.jinja', {
        'tab': 'about',
        'user_home': supplier_data.user,
        'is_home': validate_user(request.user, supplier_data.user_id),
        'supplier_data': supplier_data,
    }, context_instance=RequestContext(request, processors=[site_info]))


#服务商页表页
def show_list(request, service_id):
    #所有服务类型
    all_service_type = SupplierApi.get_service_cate()
    cookie_city = 'all'  # request.COOKIES.get('city', 'all')

    if service_id != 'all':
        service_id = int(service_id)

    supplier_data = SupplierApi.get_suppliers_post(city_id=cookie_city, page_no=1, service_id=service_id)

    return render_to_response('supplier/list.jinja', {
        'all_service_type': all_service_type,
        'service_id': service_id,
        'supplier_data': supplier_data,
        'cookie_city': cookie_city,
    }, context_instance=RequestContext(request, processors=[site_info]))


#成为服务商
@login_required
def join_supplier(request):
    service_type = None
    service_area = None
    service_cate = SupplierApi.get_service_cate()
    #查看模式
    view_module = False
    supplier_data = SupplierApi.check_data(user_id=request.user.id)

    #管理员可以通过url query查询供应商信息
    if request.user.is_admin:
        user_id = request.GET.get('uid')
        if user_id:
            view_module = True
            supplier_data = SupplierApi.check_data(user_id=int(user_id))

    if supplier_data:
        service_type = SupplierApi.get_service_type(supplier_data.id)
        service_area = SupplierApi.get_service_area(supplier_data.id)

        #供应商已经审核通过的情况下，非管理员可以查看服务商信息
        if supplier_data.enable and not request.user.is_admin:
            return HttpResponseRedirect('/u/%s' % request.user.id)

    if request.method == 'GET':
        return render_to_response('supplier/join.jinja', {
            'view_module': view_module,
            'supplier_data': supplier_data,
            'service_type': service_type,
            'service_area': service_area,
            'service_cate': service_cate
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        company = request.POST.get('company')
        service_id = request.POST.get('service-id').split(',')
        service_area_id = request.POST.get('area-id').split(',')
        city = request.POST.get('city-id')
        address = request.POST.get('address')
        name = request.POST.get('name')
        phone = request.POST.get('phone')
        tel = request.POST.get('tel')
        email = request.POST.get('email')
        qq = request.POST.get('qq')
        site = request.POST.get('site')
        map_point = request.POST.get('map')

        city_data = ProvinceApi.check_data(cid=int(city))
        #验证城市
        if service_id and service_area_id and city_data and company and name and phone:
            new_supplier = SupplierApi.add_supplier(
                company=company,
                city=city_data,
                address=address,
                name=name,
                tel=tel,
                phone=phone,
                site=site,
                qq=qq,
                email=email,
                user=request.user,
                map_point=map_point
            )

            #更新服务商服务类别
            SupplierApi().upate_supplier_service(new_supplier, service_id)
            #更新服务商服务范围
            SupplierApi.upate_supplier_area(new_supplier, service_area_id)

        return HttpResponseRedirect('/su/join/')